package com.webcams.core.settings;

import android.content.Context;
import android.content.SharedPreferences;

public abstract class BooleanSetting implements Setting {

	protected String name;
	protected String propertyName;
	protected Boolean defaultValue;
	protected Boolean value;
	protected Context context;
	
	public BooleanSetting(String name, String propertyName, Boolean defaultValue) {
		this.name = name;
		this.propertyName = propertyName;
		this.defaultValue = defaultValue;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Boolean getValue() {
		return value;
	}
	
	@Override
	public void loadValue(Context context){
		this.context = context;
		SharedPreferences settings = context.getSharedPreferences(SettingsManager.PREFS_NAME, 0);
		value = settings.getBoolean(getPropertyName(), defaultValue);
		onValueChanged();
	}
	
	@Override
	public String getPropertyName() {
		return propertyName;
	}

	@Override
	public void setValue(Object value) {
		if(value instanceof Boolean) {
			this.value = (Boolean) value;
			SharedPreferences settings = context.getSharedPreferences(SettingsManager.PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(getPropertyName(), (Boolean)value);
			editor.commit();
			this.onValueChanged();
		}
	}

}
